package com.zyasdk.deng;


import java.io.File;

import org.xutils.common.Callback.CancelledException;


import com.deng.myxutils.MyProgressCallBack;
import com.deng.myxutils.XUtil;

import android.app.Service;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class DownLoadService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }


    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //	String url="http://download.zhuoyian.com/upload/87345EC3FFFFFFE23E5FA7B829025FAF.apk";

        String url = intent.getExtras().getString("downUrl");
        final String savePath = Environment.getExternalStorageDirectory().getAbsolutePath() +
                "/youxizhongxing/";
        final String filepath = savePath + Md5Utils.md5(url) + ".apk";
        File f1 = new File(filepath);
        if (f1.exists()) {
            MdUtils.installApk(filepath, getApplicationContext());
        } else {
            XUtil.DownLoadFile(url, filepath, new MyProgressCallBack<File>() {
                @Override
                public void onStarted() {
                    Toast.makeText(getApplicationContext(), "开始下载", Toast.LENGTH_LONG).show();
                    super.onStarted();
                }

                @Override
                public void onCancelled(CancelledException arg0) {
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                }

                @Override
                public void onFinished() {
                }

                @Override
                public void onLoading(long total, long current,
                                      boolean isDownloading) {
                    Log.e("denganzhi11111", (current * 1.0 / total) + "");
                    Toast.makeText(getApplicationContext(), (current * 1.0 / total) + "", 1).show();
                    super.onLoading(total, current, isDownloading);
                }

                @Override
                public void onSuccess(File result) {
                    MdUtils.installApk(filepath, getApplicationContext());
                    Toast.makeText(getApplicationContext(), "xxx", 1).show();
                }
            });
        }
        return super.onStartCommand(intent, flags, startId);
    }

}
